package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.seating.model.AddSeatingModel;
import com.ls.controller.seating.model.EditIndexModel;
import com.ls.controller.seating.model.EditSeatingModel;
import com.ls.entity.*;
import com.ls.mapper.SeatingMapper;
import com.ls.service.IBookingService;
import com.ls.service.ICompanyService;
import com.ls.service.ISeatingService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.ls.constant.SystemConstants.VALID_FALSE;
import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 场地
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SeatingServiceImpl extends ServiceImpl<SeatingMapper, Seating> implements ISeatingService {
    private final ICompanyService companyService;
    private final IBookingService bookingService;
    /**通过id查询场地*/
    @Override
    public ResultData getSeatingById(Long seatingId) {
        Seating seating = getById(seatingId);
        return ResultData.successData(seating);
    }
    
    /**获取当前用户的场地列表*/
    @Override
    public ResultData getSeatingList() {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        
        // 查询当前用户的所有有效场地
        List<Seating> seatings = list(new LambdaQueryWrapper<Seating>()
                .eq(Seating::getCustomerId, customerId)
                .eq(Seating::getValidFlag, VALID_TRUE)
                .orderByAsc(Seating::getSeatingIndex)
                .orderByDesc(Seating::getCreateTime));
        
        return ResultData.successData(seatings);
    }
    /**
     * 新增场地
     */
    @Override
    public ResultData add(AddSeatingModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //根据用户id查询用户企业
        Long companyId = null;
        if (model.getCompanyId() == null) {
            Company company = companyService.getOne(new LambdaQueryWrapper<Company>().eq(Company::getCustomerId, customer.getCustomerId()));
            log.info("用户企业：{}", company);
            companyId = company.getCompanyId();
        } else {
            companyId = model.getCompanyId();
        }
        //判断当前用户是否存在重复的场地
        Seating seating1 = getOne(new LambdaQueryWrapper<Seating>()
                .eq(Seating::getCustomerId, customerId)
                .eq(Seating::getValidFlag, VALID_TRUE)
                .eq(Seating::getSeatingName, model.getSeatingName()));
        if (seating1 != null) {
            return ResultData.failure("当前用户已存在该场地！");
        }
        Seating seating = new Seating();
        BeanUtils.copyProperties(model, seating);
        seating.setCreateTime(LocalDateTime.now());
        seating.setCustomerId(customerId);
        seating.setCompanyId(companyId);
        seating.setValidFlag(VALID_TRUE);
        Boolean flag = saveOrUpdate(seating);
        if (flag) {
            return ResultData.successData(seating);
        } else {
            return ResultData.failure("新增失败");
        }
    }
    /**
     * 编辑场地
     */
    @Override
    public ResultData edit(EditSeatingModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        //判断当前用户是否存在重复的场地
        Seating seating1 = getOne(new LambdaQueryWrapper<Seating>()
                .eq(Seating::getCustomerId, customerId)
                .eq(Seating::getValidFlag, VALID_TRUE)
                .eq(Seating::getSeatingName, model.getSeatingName())
                .ne(Seating::getSeatingId, model.getSeatingId()));
        if (seating1 != null) {
            return ResultData.failure("当前用户已存在该场地！");
        }
        Seating seating = getById(model.getSeatingId());
        BeanUtils.copyProperties(model, seating);
        Boolean flag = saveOrUpdate(seating);
        if (flag) {
            return ResultData.successMsg("更新成功");
        } else {
            return ResultData.failure("更新失败");
        }
    }
    /**
     * 删除场地
     */
    @Override
    public ResultData del(Long seatingId) {
        Seating seating = getById(seatingId);
        if (seating == null){
            return ResultData.failure("该场地不存在");
        }
        seating.setValidFlag(VALID_FALSE);
        Integer bookings=bookingService.count(new LambdaQueryWrapper<Booking>()
                .eq(Booking::getSeatingId,seatingId)
                .ge(Booking::getBookingDay, LocalDate.now()));
        if (bookings>0){
            return ResultData.failure("该场地有预约，请先取消预约");
        }
        Boolean flag = saveOrUpdate(seating);
        if (flag) {
            return ResultData.successMsg("删除成功");
        } else {
            return ResultData.failure("删除失败");
        }
    }
    /**编辑场地排序*/
    @Override
    public ResultData editIndex(EditIndexModel model) {
        for (EditIndexModel.info info : model.getList()) {
            Seating seating = getById(info.getSeatingId());
            seating.setSeatingIndex(info.getSeatingIndex());
            saveOrUpdate(seating);
        }
        return ResultData.successMsg("排序更新成功");
    }
}
